// HEM interface implementation

// ----------------------------------------------------------------------------

#include "stdafx.h"

#include "Resources\DSymLoadVersion.h"

#include "DSymLoadUi.h"
#include "DSymLoadDbgHelpLocator.h"
#include "DSymLoadHem.h"

// ----------------------------------------------------------------------------

namespace DSymLoad
{

// ----------------------------------------------------------------------------

HMODULE g_hModule = nullptr;

// ----------------------------------------------------------------------------

namespace
{

// ----------------------------------------------------------------------------

HEMINFO_TAG g_HemInfo;

// ----------------------------------------------------------------------------

int HEM_API EntryPoint(HEMCALL_TAG *hemCall)
{
    _ASSERT(hemCall->cbSize >= sizeof(HEMCALL_TAG));
    if (hemCall->cbSize < sizeof(HEMCALL_TAG))
        return HEM_ERROR;
    try
    {
        CSettings Settings(g_hModule);
        Settings.Load();
        if (Settings.GetLibPath().IsEmpty())
        {
            Settings.SetLibPath(FindDbgHelp());
        }

        ShowMainWindow(Settings);
    }
    catch(const CError &Error)
    {
        ShowError(Error);
//        return HEM_ERROR;
    }

    return HEM_OK;
}

// ----------------------------------------------------------------------------

int HEM_API Unload()
{
    return HEM_OK;
}

// ----------------------------------------------------------------------------

}

// ----------------------------------------------------------------------------

int Load(__inout HIEWINFO_TAG *hiewInfo)
{
    _ASSERT(DSymLoad::g_hModule);

    static_assert(std::is_pod<HEMINFO_TAG>::value, "std::is_pod<HEMINFO_TAG>::value");
    memset(&g_HemInfo, 0, sizeof(g_HemInfo));

    g_HemInfo.cbSize = sizeof(g_HemInfo);
    g_HemInfo.sizeOfInt = sizeof(int);
    g_HemInfo.sdkVerMajor = HEM_SDK_VERSION_MAJOR;
    g_HemInfo.sdkVerMinor = HEM_SDK_VERSION_MINOR;
    g_HemInfo.hemVerMajor = DSYMLOAD_VERSION_MINOR;
    g_HemInfo.hemVerMinor = DSYMLOAD_VERSION_BUILDNO;
    g_HemInfo.hemFlag =
        HEM_FLAG_PE64 | HEM_FLAG_PE | HEM_FLAG_NE | HEM_FLAG_FILE |
        HEM_FLAG_CODE | HEM_FLAG_HEX;
    g_HemInfo.EntryPoint = &DSymLoad::EntryPoint;
    g_HemInfo.Unload = &DSymLoad::Unload;

    const char szShortName[] = "DSymLoad";
    static_assert(sizeof(szShortName) <= sizeof(g_HemInfo.shortName), "shortName overrun");
    memcpy(&g_HemInfo.shortName[0], &szShortName[0], sizeof(szShortName));

    const char szName[] = "Load debug symbols as HEIW local names";
    static_assert(sizeof(szName) <= sizeof(g_HemInfo.name), "name overrun");
    memcpy(&g_HemInfo.name[0], &szName[0], sizeof(szName));

    const char szAbout1[] = DSYMLOAD_URL_STR;
    static_assert(sizeof(szAbout1) <= sizeof(g_HemInfo.about1), "about1 overrun");
    memcpy(&g_HemInfo.about1[0], &szAbout1[0], sizeof(szAbout1));

    HiewGate_Set(hiewInfo);
    hiewInfo->hemInfo = &g_HemInfo;
    return HEM_OK;
}

// ----------------------------------------------------------------------------

}   // namespace DSymLoad

// ----------------------------------------------------------------------------

